package exchange.core;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Logger;

import exchange.util.Client;
import exchange.util.Order;
import exchange.util.ParseException;

public class Cancel {
	private static Logger log = Logger.getLogger(Cancel.class.getName());
	
	public static String cancelOrder(Client client, int orderId, LinkedList<Order> list ) throws ParseException {
		
		boolean isCancelled=false;
		Order order;
		String cancelMessage;
		
		synchronized (list) {
			Iterator<Order> it = list.iterator();
			while (it.hasNext() ){ 
				order = it.next(); 
				if (order.getClient()==client && order.getOrderId()==orderId){
					it.remove();
					isCancelled = true;
				}
			}
		}
		
		cancelMessage = isCancelled ? 
				"CANCEL;" + orderId + ";" + "SUCCESSFUL;" 
				: "CANCEL;" + orderId + ";" + "REJECTED; - not exis or already filled;";
		
		log.info(client.getName() + ", " + cancelMessage);
		return cancelMessage;
	}
}
